// 1.引入公用样式表
require("../../assets/css/index.less");
// 2.引入当前样式表
require("./home.less");
// 3. 引入轮播图
import Swiper from "../../assets/lib/swiper/swiper-bundle.min.js";
import "../../assets/lib/swiper/swiper-bundle.min.css";
// 4.引入工具函数
let dom = require("../../utils/dom.js");
let https = require("../../utils/https.js");
let local = require("../../utils/local.js");
// 判断用户是否登录
dom.isToken();
// 页面加载事件
dom.ready(function () {
	// 渲染底部Tab
	dom.render(0);
	// 轮播
	new Swiper(".swiper", {
		autoplay: true, //可选选项，自动滑动
		loop: true,
		pagination: {
			el: ".swiper-pagination",
		},
	});

	//跳转页面相关
	dom.goTo("#course .common", "training");
	dom.goTo("#run .common", "sports");
	dom.goTo("#ride .common", "cycling");
	dom.goTo("#data_badge .data", "echartsData");
	dom.goTo("#data_badge .badge", "badge");
	// 调接口获取用户信息
	function getUserInfo() {
		https.get("/api/user/info", function (res) {
			console.log(res);
			if (res.errno == 0) {
				let { badges, ranking } = res.data;
				dom.$("#rankTxt").textContent = ranking;
				dom.$("#badgeTxt").textContent = badges;
			}
		});
	}
	// 进入页面立即调用
	getUserInfo();
	// 获取用户是否已打卡的信息先渲染一次
	function isClockIn() {
		https.get("/api/user/clockInInfo", (res) => {
			if (res.errno == 0) {
				let { isClockIn, clockCount } = res.data;
				dom.$("#punchBtn").textContent = isClockIn ? "已打卡" : "今日打卡";
				dom.$("#punchTxt").textContent = clockCount;
			}
		});
	}
	// 进入页面立即调用
	isClockIn();

	// 给打卡按钮绑定点击事件
	dom.$("#punchBtn").addEventListener("click", function () {
		https.post("/api/user/clockIn", {}, (res) => {
			if (res.errno == 0) {
				alert("打卡成功");
				isClockIn();
			} else {
				alert(res.message);
			}
		});
	});
});
